﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;

public partial class back_ViewContent : System.Web.UI.Page
{
    #region Variable Declaration Block
    IDataRepository dataObject = new DataRepository();
    CDB.LinqModel.Content sContent;
    List<CDB.LinqModel.Content> sContents = new List<CDB.LinqModel.Content>();
    #endregion


    #region Page Events
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["admin"] != null)
                fillContents();
            else
                Response.Redirect("login.aspx");
        }
    }

    private void fillContents()
    {
        sContent = new CDB.LinqModel.Content(dataObject);
        sContents = sContent.getAllContents();

        if (sContents != null && sContents.Count > 0)
        {
            grdContents.Attributes.Add("style", " AlternatingRowStyle-CssClass:'alternate-row'");

            grdContents.DataSource = sContents;
            grdContents.DataBind();
        }
        else
        {
            grdContents.Attributes.Add("style", "display:none");
            divRed.InnerHtml = "<strong>Oh snap!</strong>&nbsp;No Records Found!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    #endregion

    #region Grid Events
    protected void grdContents_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label hdnUserId = (Label)e.Row.FindControl("hdnUserId");

            Button btnDelete = (Button)e.Row.FindControl("btnDelete");
            btnDelete.CommandArgument = hdnUserId.Text;

            Button btnEdit = (Button)e.Row.FindControl("btnEdit");
            btnEdit.CommandArgument = hdnUserId.Text;
        }
    }
    protected void grdContents_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        #region Delete Content
        if (e.CommandName == "DeleteContent")
        {
            sContent = new CDB.LinqModel.Content(dataObject);
            sContent.Id = Convert.ToInt32(e.CommandArgument);
            if (sContent.Id != 0)
            {
                sContent.DeleteContent();
                divRed.InnerHtml = "<strong>Deletion!</strong>&nbsp;Record Deleted Successfully!";
                divRed.Attributes.Add("class", "alert alert-error");
                fillContents();
            }
        }
        #endregion

        #region Edit Content
        if (e.CommandName == "EditContent")
        {
            Response.Redirect("AddContent.aspx?Id=" + e.CommandArgument.ToString());
        }
        #endregion
    }
    #endregion
}